package com.example.fashionshoes.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.fashionshoes.constant.JwtConst;
import com.example.fashionshoes.exception.BusinessException;
import com.example.fashionshoes.mapper.CartMapper;
import com.example.fashionshoes.pojo.po.Goods;
import com.example.fashionshoes.pojo.po.ShoppingCart;
import com.example.fashionshoes.pojo.po.UserCoupon;
import com.example.fashionshoes.service.CartService;
import com.example.fashionshoes.service.GoodsService;
import com.example.fashionshoes.utils.ThreadLocalUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, ShoppingCart> implements CartService {

    @Resource
    private CartMapper cartMapper;
    @Resource
    private GoodsService goodsService;
    @Resource
    private UserCouponService userCouponService;

    /**
     * 根据skuId移除购物车数据
     * @param skuId skuId
     */
    @Override
    public void removeCart(String skuId) {

        ShoppingCart shoppingCart = lambdaQuery().eq(ShoppingCart::getSkuId, skuId).one();
        if (shoppingCart !=null && shoppingCart.getUserCouponId() != null) {
            //使用了优惠券就返还优惠价
            userCouponService.lambdaUpdate().set(UserCoupon::getStatus,0).eq(UserCoupon::getId,shoppingCart.getUserCouponId()).update();
        }
        lambdaUpdate().eq(ShoppingCart::getSkuId,skuId).remove();
    }

    /**
     * 更新购物车数据
     * @param shoppingCart 购物车相关数据
     */
    @Override
    public void updateCart(ShoppingCart shoppingCart) {
        if (shoppingCart.getCount() == null) {
            //单选操作
        lambdaUpdate().eq(ShoppingCart::getSkuId,shoppingCart.getSkuId()).set(ShoppingCart::getSelected,shoppingCart.getSelected()).update();
        }else {
            //修改商品数量
            lambdaUpdate().eq(ShoppingCart::getSkuId,shoppingCart.getSkuId()).set(ShoppingCart::getCount,shoppingCart.getCount()).update();
        }
    }

    @Override
    public List<ShoppingCart> getByUserId(Integer userId) {
        return lambdaQuery().eq(ShoppingCart::getUserId, userId).list();
    }

    /**
     * 新增购物车数据
     * @param shoppingCart 购物车数据
     */
    @Override
    public void insert(ShoppingCart shoppingCart) throws BusinessException {
        //判断库存
        Integer goodsId = shoppingCart.getId();
        Goods goods = goodsService.lambdaQuery().eq(Goods::getId, goodsId).one();
        if (goods.getStock() <=0) {
            throw new BusinessException("库存不足！！！");
        }
        Map<String, Object> map = ThreadLocalUtil.get();
        shoppingCart.setUserId((Integer) map.get(JwtConst.USER_ID));
        shoppingCart.setCreateTime(LocalDateTime.now());
        shoppingCart.setGoodsId(goodsId);
        shoppingCart.setSkuId(shoppingCart.getSkuId());
        shoppingCart.setAttrsText(shoppingCart.getAttrsText());
        shoppingCart.setId(null);
        //将优惠券设为已使用
        userCouponService.lambdaUpdate().eq(UserCoupon::getId,shoppingCart.getUserCouponId()).set(UserCoupon::getStatus,1).update();
        save(shoppingCart);
    }
}
